package com.tz.controller;

import com.tz.entity.RightControl;
import com.tz.entity.Role;
import com.tz.entity.json.AllRights;
import com.tz.entity.json.RightState;
import com.tz.service.RoleService;
import com.tz.util.PageBean;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色权限管理
 *
 * @author Alex
 * @date 18-1-4
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    private final RoleService roleService;


    @Autowired
    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @ResponseBody
    @RequestMapping("/list")
    public PageBean<Role> list(@RequestParam("pageNow") Integer pageNow, @RequestParam("pageSize") Integer pageSize) {
        return roleService.queryRolesByPage(pageNow, pageSize);
    }

    @ResponseBody
    @RequestMapping("/add")
    public String list(@RequestBody Role role) {

        if (roleService.checkRoleName(role.getRolename()) == 0) {
            System.out.println(role);

            roleService.addRole(role);
            return "ok";
        } else {
            return "1";
        }
    }

    @ResponseBody
    @RequestMapping("/delete")
    public String delete(@RequestBody String idsStr) {
        List<Long> ids = new ArrayList<>();
        for (String id : idsStr.split(",")) {
            ids.add(Long.parseLong(id));
        }

        roleService.removeBatchRoles(ids);
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/addrights")
    public String addrights(@RequestBody String idsStr) {
        Long roleid = Long.parseLong(idsStr.split(":")[0]);
        List<Long> ids = new ArrayList<>();
        if (idsStr.split(":").length > 1) {
            for (String id : idsStr.split(":")[1].split(",")) {
                ids.add(Long.parseLong(id));
            }

        }

        roleService.changeRight(roleid, ids);
        return "ok";
    }


    @ResponseBody
    @RequestMapping("/allrights")
    public List<AllRights> allrights(@Param("roleid") Long roleid) {
        List<RightControl> allRights = roleService.getAllRights();
        List<RightControl> myRights = roleService.getRole(roleid);
        Map<String, List<RightState>> map = new HashMap<>();


        for (RightControl rc : allRights) {
            String title = rc.getCtrname().split(":")[0];

            RightState rs = new RightState();
            rs.setCtrname(rc.getCtrname().split(":")[1]);
            rs.setRightid(rc.getRightid());
            rs.setUrls(rc.getUrls());
            rs.setState(false);
            for (RightControl rr : myRights) {
                if (rr.getRightid().equals(rs.getRightid())) {
                    rs.setState(true);
                }
            }

            if (map.containsKey(title)) {
                map.get(title).add(rs);
            } else {
                List<RightState> list = new ArrayList<>();
                list.add(rs);
                map.put(title, list);
            }
        }
        List<AllRights> rights = new ArrayList<>();

        for (String key : map.keySet()) {
            AllRights ar = new AllRights();
            ar.setTitle(key);
            ar.setRights(map.get(key));
            rights.add(ar);
        }

        return rights;
    }


}
